﻿using System;
using System.Collections.Generic;
using System.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现==
    /// </summary>
    public class corecmsfoodsaledetailServices : Repository<corecmsfoodsaledetail>, IcorecmsfoodsaledetailServices
    {
        public bool InsertAsync(corecmsfoodsaledetail entity)
        {
            return base.Change<corecmsfoodsaledetail>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodsaledetail> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodsaledetail entity)
        {
            return base.Change<corecmsfoodsaledetail>().Update(a => new corecmsfoodsaledetail()
            {
                orderId = entity.orderId,
                orderDate = entity.orderDate,
                orderNo = entity.orderNo,
                corpId = entity.corpId,
                storeId = entity.storeId,
                storeName = entity.storeName,
                foodId = entity.foodId,
                foodSpecId = entity.foodSpecId,
                foodCode = entity.foodCode,
                foodName = entity.foodName,
                specName = entity.specName,
                foodCategoryId = entity.foodCategoryId,
                foodCategoryName = entity.foodCategoryName,
                price = entity.price,
                quantity = entity.quantity,
                amount = entity.amount,
                discount = entity.discount,
                discountAmount = entity.discountAmount,
                realAmount = entity.realAmount,
                departId = entity.departId,
                departName = entity.departName,
                ordertype = entity.ordertype,
                deskId = entity.deskId,
                deskName = entity.deskName,
                remark = entity.remark,
                detailId = entity.detailId,
                realPrice = entity.realPrice,
                taskAmount = entity.taskAmount,
                feedAmount = entity.feedAmount,
                reduceAmount = entity.reduceAmount,
                allAmount = entity.allAmount,
                costPrice = entity.costPrice
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodsaledetail> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodsaledetail>().Update(a => new corecmsfoodsaledetail()
                {
                    orderId = item.orderId,
                    orderDate = item.orderDate,
                    orderNo = item.orderNo,
                    corpId = item.corpId,
                    storeId = item.storeId,
                    storeName = item.storeName,
                    foodId = item.foodId,
                    foodSpecId = item.foodSpecId,
                    foodCode = item.foodCode,
                    foodName = item.foodName,
                    specName = item.specName,
                    foodCategoryId = item.foodCategoryId,
                    foodCategoryName = item.foodCategoryName,
                    price = item.price,
                    quantity = item.quantity,
                    amount = item.amount,
                    discount = item.discount,
                    discountAmount = item.discountAmount,
                    realAmount = item.realAmount,
                    departId = item.departId,
                    departName = item.departName,
                    ordertype = item.ordertype,
                    deskId = item.deskId,
                    deskName = item.deskName,
                    remark = item.remark,
                    detailId = item.detailId,
                    realPrice = item.realPrice,
                    taskAmount = item.taskAmount,
                    feedAmount = item.feedAmount,
                    reduceAmount = item.reduceAmount,
                    allAmount = item.allAmount,
                    costPrice = item.costPrice
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodsaledetail>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodsaledetail>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodsaledetail> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsaledetail> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodsaledetail> custos = new List<corecmsfoodsaledetail>();
            var where = Expressionable.Create<corecmsfoodsaledetail>();
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsaledetail> QueryAsync(long foodId)
        {
            List<corecmsfoodsaledetail> list = new List<corecmsfoodsaledetail>();
            var where = Expressionable.Create<corecmsfoodsaledetail>();
            if (foodId > 0)
            {
                where = where.And(a => a.foodId == foodId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsaledetail> SelectAll()
        {
            //var list = new corecmsfoodsaledetailServices().AsQueryable()
            //      .LeftJoin<corecmsfoodprintscheme>((a, b) => a.foodId == b.foodId)
            //      .InnerJoin<corecmsprintschemedetail>((a, b, d) => d.printSchemeId == b.printSchemeId)
            //      .Where((a, b, d) => a.batchId == batchId && d.selectedFlag == true && d.billtypeId == a.billtypeId)
            //      .Select((a, b, d) => new corecmsprinttask
            //      { }).With(SqlWith.NoLock).ToList();
            //return list;
            return null;
        }
        #endregion
    }
}